USE [QLDL]
GO

/****** Object:  StoredProcedure [dbo].[sp_ThemQuan]    Script Date: 01/03/2011 15:32:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_ThemQuan]
	@TenQuan ntext, @SoDLToiDa tinyint
as
begin
	if exists(select * from QUAN where TenQuan like @TenQuan)
		return 1
		
	declare @MaQuan nvarchar(10)
	select @MaQuan = MaQuan from QUAN order by (CAST(RIGHT(MaQuan, LEN(MaQuan) - 1) as int)) asc
	if (@MaQuan is not null)
		set @MaQuan = N'Q' + CAST((CAST(RIGHT(@MaQuan, LEN(@MaQuan) - 1) as int) + 1) as nvarchar)
	else
		set @MaQuan = N'Q1'
		
	insert into QUAN values(@MaQuan, @TenQuan, @SoDLToiDa)
	return 0
end
GO


